﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Dispatcher;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace ebMS3dotnet.Core.Configuration
{
    public class FilterElement : ConfigurationElement
    {
   
        [ConfigurationProperty("customType", DefaultValue = null, Options = ConfigurationPropertyOptions.None)]
        public string CustomType
        {
            get
            {
                return (string)base["customType"];
            }
            set
            {
                base["customType"] = value;
            }
        }

 
        [ConfigurationProperty("filter1", DefaultValue = null, Options = ConfigurationPropertyOptions.None)]
        public string Filter1
        {
            get
            {
                return (string)base["filter1"];
            }
            set
            {
                base["filter1"] = value;
            }
        }

        [ConfigurationProperty("filter2", DefaultValue = null, Options = ConfigurationPropertyOptions.None)]
        public string Filter2
        {
            get
            {
                return (string)base["filter2"];
            }
            set
            {
                base["filter2"] = value;
            }
        }

        [ConfigurationProperty("filterData", DefaultValue = null, Options = ConfigurationPropertyOptions.None)]
        public string FilterData
        {
            get
            {
                return (string)base["filterData"];
            }
            set
            {
                base["filterData"] = value;
            }
        }


        [ConfigurationProperty("filterType", DefaultValue = null, Options = ConfigurationPropertyOptions.IsRequired)]
        public FilterType FilterType
        {
            get
            {
                return (FilterType)base["filterType"];
            }
            set
            {
                if (value < FilterType.Action || value > FilterType.XPath)
                {
                    throw new ArgumentOutOfRangeException("value");
                }
                base["filterType"] = value;
            }
        }


        [ConfigurationProperty("name", DefaultValue = null, Options = ConfigurationPropertyOptions.IsRequired | ConfigurationPropertyOptions.IsKey)]
        public string Name
        {
            get
            {
                return (string)base["name"];
            }
            set
            {
                base["name"] = value;
            }
        }


        public FilterElement()
        {
        }

        private static MessageFilter CreateCustomFilter(string customType, string filterData)
        {
            if (string.IsNullOrEmpty(customType))
            {
                throw new ArgumentNullException("customType");
            }
            Type type = Type.GetType(customType, true);
            object[] objArray = new object[] { filterData };
            return (MessageFilter)Activator.CreateInstance(type, objArray);
        }

        internal MessageFilter CreateFilter(XmlNamespaceManager xmlNamespaces, FilterElementCollection filters)
        {
            MessageFilter actionMessageFilter;
            switch (this.FilterType)
            {
                case FilterType.Action:
                    {
                        actionMessageFilter = new ActionMessageFilter(new string[] { this.FilterData });
                        break;
                    }
                case FilterType.EndpointAddress:
                    {
                        actionMessageFilter = new EndpointAddressMessageFilter(new EndpointAddress(this.FilterData), false);
                        break;
                    }
                case FilterType.PrefixEndpointAddress:
                    {
                        actionMessageFilter = new PrefixEndpointAddressMessageFilter(new EndpointAddress(this.FilterData), false);
                        break;
                    }
                case FilterType.And:
                    {
                        MessageFilter messageFilter = filters[this.Filter1].CreateFilter(xmlNamespaces, filters);
                        MessageFilter messageFilter1 = filters[this.Filter2].CreateFilter(xmlNamespaces, filters);
                        actionMessageFilter = new StrictAndMessageFilter(messageFilter, messageFilter1);
                        break;
                    }
                case FilterType.Custom:
                    {
                        actionMessageFilter = FilterElement.CreateCustomFilter(this.CustomType, this.FilterData);
                        break;
                    }
                case FilterType.EndpointName:
                    {
                        actionMessageFilter = new EndpointNameMessageFilter(this.FilterData);
                        break;
                    }
                case FilterType.MatchAll:
                    {
                        actionMessageFilter = new MatchAllMessageFilter();
                        break;
                    }
                case FilterType.XPath:
                    {
                        actionMessageFilter = new XPathMessageFilter(this.FilterData, xmlNamespaces);
                        break;
                    }
                default:
                    {
                        throw new InvalidOperationException("Unknown filter type.");
                    }
            }
            return actionMessageFilter;
        }
    }
}
